package com.vincent.kdp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.vincent.kdp.Activity.WareHouseGoodsActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.bean.WareHouseGoodsBean;

import java.util.ArrayList;

/**
 * Created by jingjing on 2017/3/8.
 */

public class HourseListAdapter extends RecyclerView.Adapter<ViewHolder> implements View.OnClickListener{

    private static final int TYPE_ITEM=1;
    private static final int TYPE_FOOT=0;
    private WareHouseGoodsActivity context;
    private ArrayList<WareHouseGoodsBean.HouseData.HouseGoodsList> list;
    public HourseListAdapter(WareHouseGoodsActivity context, ArrayList<WareHouseGoodsBean.HouseData.HouseGoodsList> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public void onClick(View v) {
        if(listener!=null){
            listener.itemClick(v, (WareHouseGoodsBean.HouseData.HouseGoodsList) v.getTag());
        }
    }

    public interface HouseItemClickListener{
        void itemClick(View v, WareHouseGoodsBean.HouseData.HouseGoodsList data);
    }

    private HouseItemClickListener listener;
    public void setHouseItemClickListener(HouseItemClickListener listener){
        this.listener=listener;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_ITEM){
            View view = LayoutInflater.from(context).inflate(R.layout.house_list_adapter, parent,
                    false);
            view.setOnClickListener(this);
            return new ItemViewHolder(view);
        }else if(viewType==TYPE_FOOT){
            View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
                    false);
            return new FooterViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(position<list.size()){
            ItemViewHolder holderView= (ItemViewHolder) holder;
            holderView.house_name.setText(list.get(position).getName());
            holderView.house_price.setText(list.get(position).getPrice());
            holderView.house_hand.setText(list.get(position).getHandInch());
            holderView.itemView.setTag(list.get(position));
        }else{
            if(holder instanceof FooterViewHolder){
                FooterViewHolder footViewHolder=(FooterViewHolder)holder;
                if(!context.hasNext){
                    footViewHolder.progressBar.setVisibility(View.INVISIBLE);
                    footViewHolder.textView.setText("没有更多数据");
                }else{
                    footViewHolder.progressBar.setVisibility(View.VISIBLE);
                    footViewHolder.textView.setText("加载中...");
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOT;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return list.size() == 0 ? 0 : list.size() + 1;
    }

    class ItemViewHolder extends ViewHolder{
        TextView house_name;
        TextView house_price;
        TextView house_hand;
        public ItemViewHolder(View itemView) {
            super(itemView);
            house_name= (TextView) itemView.findViewById(R.id.house_name);
            house_price= (TextView) itemView.findViewById(R.id.house_price);
            house_hand= (TextView) itemView.findViewById(R.id.house_hand);

        }
    }

    class FooterViewHolder extends ViewHolder{
        ProgressBar progressBar;
        TextView textView;

        public FooterViewHolder(View view) {
            super(view);
            progressBar=(ProgressBar) view.findViewById(R.id.progressBar);
            textView=(TextView) view.findViewById(R.id.footText);
        }
    }

}
